qui(
    function (require, exports, module) {
        VODNew.Advert = (function () {
            function getSubmitAdData() {
                var advertAccurateData = VODNew.AccurateFrame.getAdAccurate();
                var d_sTime = $('input[name=d_sTime]').val();
                var d_eTime = $('input[name=d_eTime]').val();
                var d_title = $('input[name=d_videoName]').val();
                return {
                    'index': VODNew.Dao.settings.index,
                    'sTime': d_sTime,
                    'eTime': d_eTime,
                    'title': d_title,
                    'channel': '',
                    'album': '',
                    'pic': '',
                    'head': advertAccurateData.head,
                    'end': advertAccurateData.end,
                    'headSetted': advertAccurateData.headSetted,
                    'endSetted': advertAccurateData.endSetted,
                    'headPic': advertAccurateData.headPic,
                    'endPic': advertAccurateData.endPic
                }
            }

            function addAdvertProgress(progressObj, skipCheck) {
                skipCheck = skipCheck == null ? false : skipCheck == true
                progressObj.doBefore();
                if (progressObj.doCheck(skipCheck)) {
                    progressObj.doSubmit(progressObj.doFinal, progressObj.doCallBack);
                } else {
                    progressObj.doFinal();
                }
            }

            function del_submit(forceFlag) {
                var dataSubmit = getSubmitAdData();
                var data = {
                    '起始帧': dataSubmit.sTime,
                    '结束帧': dataSubmit.eTime,
                    '标题': dataSubmit.title
                };
                var needRemind = VODNew.Util.checkAdvertAffectDot(dataSubmit.sTime, dataSubmit.eTime);
                var progressObj = {
                    doBefore: function () {
                        $('#d_addInfo').attr('disabled', 'disabled');
                    },
                    doCheck: function (skipCheck) {
                        if (skipCheck) {
                            return true;
                        }
                        if (!VODNew.Util.checkStAndEt(dataSubmit.sTime, dataSubmit.eTime)) {
                            console.log("interrupt checkLiveType checkRepeat")
                            return false;
                        }
                        if (!VODNew.Util.validAddInfo(data)) {
                            return false;
                        }
                        if(!checkAdvertCross(dataSubmit)){
                            VODNew.Plugin.tips("error", VODNew.Config.REMIND_MSG_INFO.ADVERT_CROSS);
                            return false;
                        }
                        if (needRemind) {
                            SystemDialog.queryConfirm(VODNew.Config.REMIND_MSG_INFO.ADVERT_SUBMIT, function (flag) {
                                if (flag == true) {
                                    VODNew.Advert.del_submit(true);
                                }
                            }, {"btn_ok": "确认提交广告"})
                            return false;
                        }
                        return true;
                    },
                    doSubmit: function (revertHandler, callback) {
                        VODNew.Config.Global.d_sty2Data.push(dataSubmit);
                        var tempHandler = function () {
                            $('input[name=d_sTime]').val('');
                            $('input[name=d_eTime]').val('');
                            VODNew.Dao.settings.index++;
                            VODNew.AccurateFrame.adTipRemove(); // 广告左右标识删除
                            VODNew.Plugin.tips("success", '删除成功');
                            if (callback != null) {
                                callback();
                            }
                        }
                        advertAddInfo(dataSubmit, tempHandler, revertHandler)
                    },
                    doFinal: function () {
                        $('#d_addInfo').removeAttr('disabled').html('提交');
                    }
                }
                if (needRemind) {
                    progressObj.doCallBack = function () {
                        VODNew.Util.doQuickFragmentSubmit();
                    }
                }
                addAdvertProgress(progressObj, forceFlag);
            }

            function checkAdvertCross(data){
                data.sTime = parseInt(data.sTime);
                data.eTime = parseInt(data.eTime);

                var advertSource = VODNew.Config.PageBeanAdvert.getSource();
                var temp = [];
                $.each(advertSource, function (i, element) {
                    for (var i = element.sTime; i <= element.eTime; i++) {
                        temp.push(i);
                    }
                })
                var uniqueArr = temp;

                temp = [];
                for (var i = data.sTime; i <= data.eTime; i++) {
                    temp.push(i);
                }
                var totalLength = uniqueArr.length + temp.length;
                $.each(temp, function (i, el) { //去重复
                    if ($.inArray(el, uniqueArr) === -1)
                        uniqueArr.push(el);
                });
                if (uniqueArr.length < totalLength) {
                    return false;
                }
                return true
            }

            function advertAddInfo(data, tempHandler, revertHandler) {
                $('#d_addInfo').html('成功');
                data.sTime = parseInt(data.sTime);
                data.eTime = parseInt(data.eTime);
                var tempList = [];
                if (VODNew.Config.ADVERT_SUPPORT) {
                    if (data.headSetted) {
                        tempList.push({id: data.headPic, type: 0, offset: data.head})
                    }
                    if (data.endSetted) {
                        tempList.push({id: data.endPic, type: -1, offset: data.end })
                    }
                }
                var sendData = null;
                if (tempList.length > 0) {
                    sendData = {key: VODNew.Dao.settings.taskid, list: JSON.stringify(tempList), from_client: true};
                }
                VODNew.Dao.doAdvertCutAdd(sendData, function (rdata) {
                    revertHandler();
                    var suc = rdata != null && rdata.code == VODNew.Config.CODE_SUC
                    if (suc) {
                        VODNew.Config.PageBeanAdvert.addNewOne(data);
                        VODNew.Util.updateGridAdvert();
                        accountDelList();
                        tempHandler();
                        if (sendData != null) {
                            VODNew.Util.logSubmit(VODNew.Config.VOD_LOG_TYPE.addAdvertAccurate, sendData, rdata, VODNew.Dao.getDotData().doadadd);
                        }
                    } else {
                        if (sendData != null) {
                            VODNew.Util.logSubmit(VODNew.Config.VOD_LOG_TYPE.addAdvertAccurate, sendData, {error: "提交广告精确化失败!"}, VODNew.Dao.getDotData().doadadd);
                        }
                    }
                })
            }

            function advertRemoveInfo(findData, tempHandler) {
                var tempList = [];
                if (VODNew.Config.ADVERT_SUPPORT) {
                    if (findData.headSetted) {
                        tempList.push(findData.headPic);
                    }
                    if (findData.endSetted) {
                        tempList.push(findData.endPic);
                    }
                }
                var sendData = null;
                if (tempList.length > 0) {
                    sendData = {key: VODNew.Dao.settings.taskid, list: JSON.stringify(tempList), from_client: true};
                }
                VODNew.Dao.doAdvertCutDel(sendData, function (rdata) {
                    var suc = rdata != null && rdata.code == VODNew.Config.CODE_SUC
                    if (suc) {
                        VODNew.Config.PageBeanAdvert.delOne(findData)
                        VODNew.Util.updateGridAdvert();
                        accountDelList();
                        tempHandler();
                        if (sendData != null) {
                            VODNew.Util.logSubmit(VODNew.Config.VOD_LOG_TYPE.delAdvertAccurate, sendData, rdata, VODNew.Dao.getDotData().doaddel);
                        }
                    } else {
                        if (sendData != null) {
                            VODNew.Util.logSubmit(VODNew.Config.VOD_LOG_TYPE.delAdvertAccurate, sendData, {error: "删除广告精确化失败!"}, VODNew.Dao.getDotData().doaddel);
                        }
                    }
                })
            }

            function delAdvert(rowId) {
                var findData = VODNew.Config.PageBeanAdvert.findData("index", rowId);
                if (findData != null) {
                    var needCheck = VODNew.Util.checkAdvertAffectDot(findData.sTime, findData.eTime);
                    var progressObj = {
                        doBefore: function () {
                        },
                        doCheck: function (skipCheck) {
                            return true;
                        },
                        doSubmit: function (revertHandler, callback) {
                            SystemDialog.queryConfirm(needCheck ? VODNew.Config.REMIND_MSG_INFO.ADVERT_DEL : VODNew.Config.REMIND_MSG_INFO.NORMAL, function (flag) {
                                if (flag === true) {
                                    var tempHandler = function () {
                                        if (callback != null) {
                                            callback();
                                        }
                                    }
                                    advertRemoveInfo(findData, tempHandler)
                                }
                            }, {"btn_ok": "确认删除广告"})
                        },
                        doFinal: function () {
                        }
                    }
                    if (needCheck) {
                        progressObj.doCallBack = function () {
                            VODNew.Util.doQuickFragmentSubmit();
                        }
                    }
                    addAdvertProgress(progressObj, false)
                }
            }

            function accountDelList() {
                var arr3 = [];
                var settings = VODNew.Dao.settings;
                var advertSource = VODNew.Config.PageBeanAdvert.getSource();
                $.each(advertSource, function (i, element) {
                    if(element.headSetted != true){
                        arr3.push(element.sTime);
                    }
                    for (var i = element.sTime + 1; i < element.eTime; i++) {
                        arr3.push(i);
                    }
                    if(element.endSetted != true){
                        arr3.push(element.eTime);
                    }
                })
                var uniqueArr3 = [];
                $.each(arr3, function (i, el) { //去重复
                    if ($.inArray(el, uniqueArr3) === -1)
                        uniqueArr3.push(el);
                });
                settings.delList = uniqueArr3;
            }

            return {
                del_submit: del_submit,
                delAdvert: delAdvert
            }
        })()
    }
);